using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Skywave.Windows.Forms
{
    public enum MessageBoxHelperDefaultButton
    {
        Button1 = 0,
        Button2 = 1,
        Button3 = 2
    }
    public enum MessageBoxHelperIcon
    {
        None = 0,
        Critical = 1,
        Information = 2,
        Exclamation = 3,
        Question = 4
    }
    public enum MessageBoxHelperButtons
    {
        OK = 0,
        OKCancel = 1,
        AbortRetryIgnore = 2,
        RetryCancel = 3,
        YesNo = 4,
        YesNoCancel = 5
    }
    public enum MessageBoxHelperOptions
    {
        None = 0,
        AlignRightToLeft = 1,
        RightToLeftOrder = 2
    }
    public class MessageBoxHelper
    {
        public static DialogResult Show(string prompt
            , string title
            , MessageBoxHelperButtons buttons
            , MessageBoxHelperIcon icon
            , MessageBoxHelperDefaultButton defaultButton
            , MessageBoxHelperOptions option
            , Form owner
            , FormStartPosition windowsPosition
            , int Left, int Top)
        {
            MessageBoxHelper_Form frmMaMessageBox = new MessageBoxHelper_Form();

            if (option != MessageBoxHelperOptions.None)
            {
                frmMaMessageBox.lblPrompt.RightToLeft = ((option & MessageBoxHelperOptions.RightToLeftOrder) == MessageBoxHelperOptions.RightToLeftOrder) ? RightToLeft.Yes : RightToLeft.No;
                frmMaMessageBox.lblPrompt.TextAlign = ((option & MessageBoxHelperOptions.AlignRightToLeft) == MessageBoxHelperOptions.AlignRightToLeft) ? System.Drawing.ContentAlignment.MiddleRight : System.Drawing.ContentAlignment.BottomLeft;
            }
            frmMaMessageBox.Text = title;
            Graphics myGr = frmMaMessageBox.CreateGraphics();
            frmMaMessageBox.lblPrompt.Size = myGr.MeasureString(prompt, new Font(frmMaMessageBox.lblPrompt.Font.FontFamily, frmMaMessageBox.lblPrompt.Font.Size + 1)).ToSize();
            //frmMaMessageBox.lblPrompt.Width =(int) myGr.MeasureString(prompt, new Font("Tahoma", 8)).Width;
            frmMaMessageBox.lblPrompt.Text = prompt;

            //    'Icon!
            if (icon == MessageBoxHelperIcon.Critical)
            { frmMaMessageBox.pbIcon.Image = SystemIcons.Error.ToBitmap(); }
            else if (icon == MessageBoxHelperIcon.Exclamation)
            { frmMaMessageBox.pbIcon.Image = SystemIcons.Exclamation.ToBitmap(); }
            else if (icon == MessageBoxHelperIcon.Question)
            { frmMaMessageBox.pbIcon.Image = SystemIcons.Question.ToBitmap(); }
            else if (icon == MessageBoxHelperIcon.Information)
            { frmMaMessageBox.pbIcon.Image = SystemIcons.Information.ToBitmap(); }

            frmMaMessageBox.Width = frmMaMessageBox.pbIcon.Width + frmMaMessageBox.lblPrompt.Width + 50;
            frmMaMessageBox.Height = frmMaMessageBox.lblPrompt.Height + frmMaMessageBox.cmdAbort.Height + 75;

            if (buttons == MessageBoxHelperButtons.AbortRetryIgnore)
            {
                if (frmMaMessageBox.Width < (3 * frmMaMessageBox.cmdAbort.Width) + 20) frmMaMessageBox.Width = (3 * frmMaMessageBox.cmdAbort.Width) + 20;
                frmMaMessageBox.cmdAbort.Top = frmMaMessageBox.Height - 2 * frmMaMessageBox.cmdAbort.Height - 12;
                frmMaMessageBox.cmdAbort.Left = (int)(frmMaMessageBox.Width / 2 - (3 * frmMaMessageBox.cmdAbort.Width + 14) / 2);
                frmMaMessageBox.cmdRetry.Top = frmMaMessageBox.cmdAbort.Top;
                frmMaMessageBox.cmdRetry.Left = frmMaMessageBox.cmdAbort.Left + frmMaMessageBox.cmdAbort.Width + 5;
                frmMaMessageBox.cmdIgnore.Top = frmMaMessageBox.cmdRetry.Top;
                frmMaMessageBox.cmdIgnore.Left = frmMaMessageBox.cmdRetry.Left + frmMaMessageBox.cmdRetry.Width + 5;
                frmMaMessageBox.cmdAbort.Visible = true;
                frmMaMessageBox.cmdRetry.Visible = true;
                frmMaMessageBox.cmdIgnore.Visible = true;
                if (defaultButton == MessageBoxHelperDefaultButton.Button3)
                {
                    frmMaMessageBox.cmdAbort.TabIndex = 2;
                    frmMaMessageBox.cmdRetry.TabIndex = 3;
                    frmMaMessageBox.cmdIgnore.TabIndex = 1;
                }
                else if (defaultButton == MessageBoxHelperDefaultButton.Button2)
                {
                    frmMaMessageBox.cmdAbort.TabIndex = 3;
                    frmMaMessageBox.cmdRetry.TabIndex = 1;
                    frmMaMessageBox.cmdIgnore.TabIndex = 2;
                }
                else
                {
                    frmMaMessageBox.cmdAbort.TabIndex = 1;
                    frmMaMessageBox.cmdRetry.TabIndex = 2;
                    frmMaMessageBox.cmdIgnore.TabIndex = 3;
                }
            }
            else if (buttons == MessageBoxHelperButtons.OKCancel)
            {
                if (frmMaMessageBox.Width < (2 * frmMaMessageBox.cmdOK.Width) + 20) frmMaMessageBox.Width = (2 * frmMaMessageBox.cmdOK.Width) + 20;
                frmMaMessageBox.cmdOK.Top = frmMaMessageBox.Height - 2 * frmMaMessageBox.cmdOK.Height - 12;
                frmMaMessageBox.cmdOK.Left = (int)(frmMaMessageBox.Width / 2 - (2 * frmMaMessageBox.cmdOK.Width + 14) / 2);
                frmMaMessageBox.cmdCancel.Top = frmMaMessageBox.cmdOK.Top;
                frmMaMessageBox.cmdCancel.Left = frmMaMessageBox.cmdOK.Left + frmMaMessageBox.cmdOK.Width + 5;
                frmMaMessageBox.cmdOK.Visible = true;
                frmMaMessageBox.cmdCancel.Visible = true;
                if (defaultButton == MessageBoxHelperDefaultButton.Button2)
                {
                    frmMaMessageBox.cmdOK.TabIndex = 2;
                    frmMaMessageBox.cmdCancel.TabIndex = 1;
                }
                else
                {
                    frmMaMessageBox.cmdOK.TabIndex = 1;
                    frmMaMessageBox.cmdCancel.TabIndex = 2;
                }

            }
            else if (buttons == MessageBoxHelperButtons.RetryCancel)
            {
                if (frmMaMessageBox.Width < (2 * frmMaMessageBox.cmdRetry.Width) + 20) frmMaMessageBox.Width = (2 * frmMaMessageBox.cmdRetry.Width) + 20;
                frmMaMessageBox.cmdCancel.Width = frmMaMessageBox.cmdRetry.Width;
                frmMaMessageBox.cmdRetry.Top = frmMaMessageBox.Height - 2 * frmMaMessageBox.cmdRetry.Height - 12;
                frmMaMessageBox.cmdRetry.Left = (int)(frmMaMessageBox.Width / 2 - (2 * frmMaMessageBox.cmdRetry.Width + 14) / 2);
                frmMaMessageBox.cmdCancel.Top = frmMaMessageBox.cmdRetry.Top;
                frmMaMessageBox.cmdCancel.Left = frmMaMessageBox.cmdRetry.Left + frmMaMessageBox.cmdRetry.Width + 5;
                frmMaMessageBox.cmdRetry.Visible = true;
                frmMaMessageBox.cmdCancel.Visible = true;
                if (defaultButton == MessageBoxHelperDefaultButton.Button2)
                {
                    frmMaMessageBox.cmdRetry.TabIndex = 2;
                    frmMaMessageBox.cmdCancel.TabIndex = 1;
                }
                else
                {
                    frmMaMessageBox.cmdRetry.TabIndex = 1;
                    frmMaMessageBox.cmdCancel.TabIndex = 2;
                }
            }
            else if (buttons == MessageBoxHelperButtons.YesNo)
            {
                if (frmMaMessageBox.Width < (2 * frmMaMessageBox.cmdYes.Width) + 20) frmMaMessageBox.Width = (2 * frmMaMessageBox.cmdYes.Width) + 20;
                frmMaMessageBox.cmdYes.Top = frmMaMessageBox.Height - 2 * frmMaMessageBox.cmdYes.Height - 12;
                frmMaMessageBox.cmdYes.Left = (int)(frmMaMessageBox.Width / 2 - (2 * frmMaMessageBox.cmdYes.Width + 14) / 2);
                frmMaMessageBox.cmdNo.Top = frmMaMessageBox.cmdYes.Top;
                frmMaMessageBox.cmdNo.Left = frmMaMessageBox.cmdYes.Left + frmMaMessageBox.cmdYes.Width + 5;
                frmMaMessageBox.cmdYes.Visible = true;
                frmMaMessageBox.cmdNo.Visible = true;
                if (defaultButton == MessageBoxHelperDefaultButton.Button2)
                {
                    frmMaMessageBox.cmdYes.TabIndex = 2;
                    frmMaMessageBox.cmdNo.TabIndex = 1;
                }
                else
                {
                    frmMaMessageBox.cmdYes.TabIndex = 1;
                    frmMaMessageBox.cmdNo.TabIndex = 2;
                }
            }
            else if (buttons == MessageBoxHelperButtons.YesNoCancel)
            {
                if (frmMaMessageBox.Width < (3 * frmMaMessageBox.cmdYes.Width) + 20) frmMaMessageBox.Width = (3 * frmMaMessageBox.cmdYes.Width) + 20;
                frmMaMessageBox.cmdYes.Top = frmMaMessageBox.Height - 2 * frmMaMessageBox.cmdYes.Height - 12;
                frmMaMessageBox.cmdYes.Left = (int)(frmMaMessageBox.Width / 2 - (3 * frmMaMessageBox.cmdYes.Width + 14) / 2);
                frmMaMessageBox.cmdNo.Top = frmMaMessageBox.cmdYes.Top;
                frmMaMessageBox.cmdNo.Left = frmMaMessageBox.cmdYes.Left + frmMaMessageBox.cmdYes.Width + 5;
                frmMaMessageBox.cmdCancel.Top = frmMaMessageBox.cmdNo.Top;
                frmMaMessageBox.cmdCancel.Left = frmMaMessageBox.cmdNo.Left + frmMaMessageBox.cmdNo.Width + 5;
                frmMaMessageBox.cmdYes.Visible = true;
                frmMaMessageBox.cmdNo.Visible = true;
                frmMaMessageBox.cmdCancel.Visible = true;
                if (defaultButton == MessageBoxHelperDefaultButton.Button3)
                {
                    frmMaMessageBox.cmdYes.TabIndex = 2;
                    frmMaMessageBox.cmdNo.TabIndex = 3;
                    frmMaMessageBox.cmdCancel.TabIndex = 1;
                }
                else if (defaultButton == MessageBoxHelperDefaultButton.Button2)
                {
                    frmMaMessageBox.cmdYes.TabIndex = 3;
                    frmMaMessageBox.cmdNo.TabIndex = 1;
                    frmMaMessageBox.cmdCancel.TabIndex = 2;
                }
                else
                {
                    frmMaMessageBox.cmdYes.TabIndex = 1;
                    frmMaMessageBox.cmdNo.TabIndex = 2;
                    frmMaMessageBox.cmdCancel.TabIndex = 3;
                }
            }
            else
            {
                frmMaMessageBox.cmdOK.Top = frmMaMessageBox.Height - 2 * frmMaMessageBox.cmdOK.Height - 12;
                frmMaMessageBox.cmdOK.Left = (int)(frmMaMessageBox.Width / 2 - (1 * frmMaMessageBox.cmdOK.Width + 14) / 2);
                if (frmMaMessageBox.Width < (1 * frmMaMessageBox.cmdOK.Width) + 20) frmMaMessageBox.Width = (1 * frmMaMessageBox.cmdOK.Width) + 20;
                frmMaMessageBox.cmdOK.Visible = true;
                frmMaMessageBox.cmdOK.TabIndex = 1;
            }
            if (windowsPosition == FormStartPosition.Manual)
            {
                if ((Left == -1) || (Top == -1)) frmMaMessageBox.StartPosition = FormStartPosition.CenterParent;
                else
                {
                    frmMaMessageBox.Left = Left;
                    frmMaMessageBox.Top = Top;
                    frmMaMessageBox.StartPosition = windowsPosition;
                }
            }
            else
            {
                frmMaMessageBox.StartPosition = windowsPosition;
            }
            if (owner != null)
                return frmMaMessageBox.ShowDialog(owner);
            else
                return frmMaMessageBox.ShowDialog();
        }

        public static DialogResult Show(string prompt
            , string title
            , MessageBoxHelperButtons buttons
            , MessageBoxHelperIcon icon
            , MessageBoxHelperDefaultButton defaultButton
            , MessageBoxHelperOptions option
            , Form owner)
        {
            return Show(prompt, title, buttons, icon, defaultButton, option, owner, FormStartPosition.CenterParent, -1, -1);
        }

        public static DialogResult Show(string prompt
            , string title
            , MessageBoxHelperButtons buttons
            , MessageBoxHelperIcon icon
            , MessageBoxHelperDefaultButton defaultButton
            , MessageBoxHelperOptions option
            , FormStartPosition windowsPosition
            , int Left, int Top)
        {
            return Show(prompt, title, buttons, icon, defaultButton, option, null, windowsPosition, -1, -1);
        }

        public static DialogResult Show(string prompt
            , string title
            , MessageBoxHelperButtons buttons
            , MessageBoxHelperIcon icon
            , MessageBoxHelperDefaultButton defaultButton
            , MessageBoxHelperOptions option
            , FormStartPosition windowsPosition)
        {
            return Show(prompt, title, buttons, icon, defaultButton, option, windowsPosition, -1, -1);
        }

        public static DialogResult Show(string prompt
            , string title
            , MessageBoxHelperButtons buttons
            , MessageBoxHelperIcon icon
            , MessageBoxHelperDefaultButton defaultButton
            , MessageBoxHelperOptions option)
        {
            return Show(prompt, title, buttons, icon, defaultButton, option, FormStartPosition.CenterParent);
        }

        public static DialogResult Show(string prompt
            , string title
            , MessageBoxHelperButtons buttons
            , MessageBoxHelperIcon icon
            , MessageBoxHelperDefaultButton defaultButton)
        {
            return Show(prompt, title, buttons, icon, defaultButton, MessageBoxHelperOptions.None);
        }

        public static DialogResult Show(string prompt
            , string title
            , MessageBoxHelperButtons buttons
            , MessageBoxHelperIcon icon)
        {
            return Show(prompt, title, buttons, icon, MessageBoxHelperDefaultButton.Button1);
        }

        public static DialogResult Show(string prompt
            , string title
            , MessageBoxHelperButtons buttons)
        {
            return Show(prompt, title, buttons, MessageBoxHelperIcon.None);
        }

        public static DialogResult Show(string prompt
            , string title)
        {
            return Show(prompt, title, MessageBoxHelperButtons.OK);
        }

        public static DialogResult Show(string prompt)
        {
            return Show(prompt, "");
        }
    }
}
